package com.qf.aop2204.controller;

import com.qf.aop2204.aop.CalculateTime;
import com.qf.aop2204.aop.NullValidate;
import com.qf.aop2204.vo.User;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;

@RestController
@RequestMapping("/test")
public class TestController {
    @GetMapping("/1")
    public String test1(){
        System.out.println("进入TestController->test1");
        return "test1";
    }
    @PostMapping("/2")
    @CalculateTime(logfilepath = "D:/log.txt",time = 15)
    public String test2() throws InterruptedException {
        System.out.println("进入TestController->test2");
        Thread.sleep(10);
        return "test2";
    }

    @PostMapping("/3")
    @NullValidate
    public User test3(String name,String password) throws InterruptedException {
        User user = new User();
        user.setName(name);
        user.setPassword(password);
        return user;
    }
    @PostMapping("/4")
    public String test4(HttpServletRequest request, String name, String password){
        Enumeration<String> parameterNames = request.getParameterNames();
        if (parameterNames!=null) {
            while (parameterNames.hasMoreElements()) {
                System.out.println("请求参数：" + parameterNames.nextElement());
            }
        }else {
            System.out.println("没有请求参数");
        }

        //System.out.println("所有的请求头和请求体：");
        Enumeration<String> headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()){
            String nextElement = headerNames.nextElement();//获取headerNames集合中的请求头
            String header2 = request.getHeader(nextElement);//通过请求头得到请求体
            System.err.println("请求头==key:" + nextElement+"\n请求体==value:" + header2);
        }
        System.out.println("请求的url:"+request.getRequestURL());
        return "test4";
    }
}
